package Client;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import nl.captcha.Captcha;

@SuppressWarnings("serial")
public class OurCaptcha extends JPanel implements ActionListener{
	
	Captcha c;
	JTextField input;
	JLabel label;
	JButton refersh;
	ImagePanel imgPanel;
	
	public OurCaptcha() {
		c = new Captcha.Builder(200, 50).addText().addNoise().addNoise().addNoise().addBackground().build();
		Image img = c.getImage();
		imgPanel = new ImagePanel(img);
		imgPanel.setBounds(0, 0, 200, 50);
		label = new JLabel("Type the word in the image :- ");
		label.setBounds(0, 55, 200, 30);
		input = new JTextField();
		input.setBounds(0, 90, 200, 30);
		refersh = new JButton("Refresh Image");
		refersh.setBounds(0, 125, 130, 30);
		refersh.addActionListener(this);
		setLayout(null);
		setBackground(Color.WHITE);
		add(imgPanel);
		add(label);
		add(input);
		add(refersh);
	}

	public String getText() {
		return input.getText();
	}
	
	public boolean validateAnswer(String written) {
		if(c.getAnswer().equals(written))
		{
			return true;
		}
		return false;
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand().equals("Refresh Image"))
		{
			System.out.println("karim");
			c = new Captcha.Builder(200, 50).addText().addNoise().addNoise().addNoise().addBackground().build();
			System.out.println("karim1 " + c.getAnswer());
			Image img = c.getImage();
			remove(this.imgPanel);
			ImagePanel imgPanel = new ImagePanel(img);
			imgPanel.setBounds(0, 0, 200, 50);
			this.imgPanel = imgPanel;
			this.repaint();
			add(imgPanel);
			this.repaint();
		}
	}
}
